package com.shuoyue.adven.main.bussiness;

import android.content.Context;
import android.graphics.Color;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.shuoyue.adven.R;
import com.shuoyue.adven.entity.Adv;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)

public class PropertyAdapter extends PagerAdapter {
    Context mContext;
    /**
     * View缓存池，从ViewPager中移除的item将会存到这里面，用来复用
     */
    private List<View> mViewPoolVideo = new ArrayList<>();
    private List<View> mViewPoolImg = new ArrayList<>();
    private List<View> mViewPoolText = new ArrayList<>();

    /**
     * 数据源
     */
    private List<Adv> medias = new ArrayList<>();
    private ImageView.ScaleType imgScaleType = ImageView.ScaleType.CENTER_CROP;

    public PropertyAdapter(Context context, List<Adv> videoBeans) {
        mContext = context;
        if (videoBeans != null)
            this.medias.addAll(videoBeans);
    }

    @Override
    public int getCount() {
        return medias == null || medias.size() == 0 ? 0 : Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    public Adv getItem(int position) {
        if (medias != null) {
            return medias.get(position % medias.size());
        }
        return null;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        Context context = container.getContext();
        Adv item = medias.get(position % medias.size());
        if (item.getType() == Adv.TYPE_VIDEO) {
            View view = null;
            if (mViewPoolVideo.size() > 0) {//取第一个进行复用
                view = mViewPoolVideo.get(0);
                mViewPoolVideo.remove(0);
            }

            PropertyAdapter.VideoHolder viewHolder;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(R.layout.item_video2, container, false);
                viewHolder = new PropertyAdapter.VideoHolder(view);
                view.setTag(viewHolder);
            } else {
                viewHolder = (PropertyAdapter.VideoHolder) view.getTag();
            }
            if (mContext != null) {
                Glide.with(context)
                        .load(item.getVideoUrl())
                        .error(R.mipmap.img_loading_gray)
                        .placeholder(R.mipmap.img_loading_gray)
                        .into(viewHolder.thumb);
            }
            viewHolder.mPosition = position;
            container.addView(view);
            return view;
        } else if (Adv.TYPE_IMG == item.getType()) {
            View view = null;
            if (mViewPoolImg.size() > 0) {//取第一个进行复用
                view = mViewPoolImg.get(0);
                mViewPoolImg.remove(0);
            }

            PropertyAdapter.ImgHolder viewHolder;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(R.layout.item_image_pager, container, false);
                viewHolder = new PropertyAdapter.ImgHolder(view);
                view.setTag(viewHolder);
            } else {
                viewHolder = (PropertyAdapter.ImgHolder) view.getTag();
            }

//            View view = LayoutInflater.from(context).inflate(R.layout.item_image_pager, container, false);
//            PropertyAdapter.ImgHolder imgHolder = new PropertyAdapter.ImgHolder(view);
            viewHolder.img.setScaleType(imgScaleType);
            if (mContext != null) {
                Glide.with(context)
                        .load(item.getImgUrl())
                        .error(R.mipmap.img_loading_gray)
                        .placeholder(R.mipmap.img_loading_gray)
                        .into(viewHolder.img);
            }
            viewHolder.mPosition = position;
            container.addView(view);
            return view;
        } else {
            View view = null;
            if (mViewPoolText.size() > 0) {//取第一个进行复用
                view = mViewPoolText.get(0);
                mViewPoolText.remove(0);
            }

            PropertyAdapter.TextHolder viewHolder;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(R.layout.item_property_text, container, false);
                viewHolder = new PropertyAdapter.TextHolder(view);
                view.setTag(viewHolder);
            } else {
                viewHolder = (PropertyAdapter.TextHolder) view.getTag();
            }

//            View view = LayoutInflater.from(context).inflate(R.layout.item_text, container, false);
//            PropertyAdapter.TextHolder textHolder = new PropertyAdapter.TextHolder(view);
            viewHolder.textView.setText(item.getFontContent());
            if (item.getFontStyle() == 1) {//1：红底白字 2：蓝底白字 3：绿底黑字 4：黄底黑字
                viewHolder.textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.red));
                viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            }
            if (item.getFontStyle() == 2) {//1：红底白字 2：蓝底白字 3：绿底黑字 4：黄底黑字
                viewHolder.textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.blue));
                viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            }
            if (item.getFontStyle() == 3) {//1：红底白字 2：蓝底白字 3：绿底黑字 4：黄底黑字
                viewHolder.textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.green));
                viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            }
            if (item.getFontStyle() == 4) {//1：红底白字 2：蓝底白字 3：绿底黑字 4：黄底黑字
                viewHolder.textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.yellow));
                viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            }
            if (!TextUtils.isEmpty(item.getTextColor()) && !TextUtils.isEmpty(item.getTextColor())) {
                viewHolder.textView.setBackgroundColor(Color.parseColor("#" + item.getBackGroundColor()));
                viewHolder.textView.setTextColor(Color.parseColor("#" + item.getTextColor()));
            }
            container.addView(view);
            return view;
        }
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        View itemView = (View) object;
        container.removeView(itemView);
        if (itemView.getTag() != null && itemView.getTag() instanceof PropertyAdapter.VideoHolder) {
            //保存起来用来复用
            mViewPoolVideo.add(itemView);
        }
        if (itemView.getTag() != null && itemView.getTag() instanceof PropertyAdapter.ImgHolder) {
            //保存起来用来复用
            mViewPoolImg.add(itemView);
        }
        if (itemView.getTag() != null && itemView.getTag() instanceof PropertyAdapter.TextHolder) {
            //保存起来用来复用
            mViewPoolText.add(itemView);
        }
    }


    public class VideoHolder {

        public int mPosition;

        public ImageView thumb;

        VideoHolder(View itemView) {
            thumb = itemView.findViewById(R.id.thumb);
        }


    }

    public class ImgHolder {

        public int mPosition;
        public ImageView img;

        ImgHolder(View itemView) {
            img = itemView.findViewById(R.id.img);
        }


    }

    public class TextHolder {

        public int mPosition;
        public TextView textView;

        TextHolder(View itemView) {
            textView = itemView.findViewById(R.id.textview);
        }
    }


    View mCurrentView;

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        mCurrentView = (View) object;
    }

    public void setImgScaleType(ImageView.ScaleType imgScaleType) {
        this.imgScaleType = imgScaleType;
    }

    public View getPrimaryItem() {
        return mCurrentView;
    }

    public void resetData(List<Adv> newData) {
        if (medias == null) {
            medias = new ArrayList<>();
        }
        medias.clear();
        medias.addAll(newData);
        notifyDataSetChanged();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

}
